#define _GNU_SOURCE
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define handle_error_en(en, msg) \
           do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

int
main(void)
{
	 int s;
	 cpu_set_t cpuset;
	 pthread_t thread;

	 thread = pthread_self();

	 /* Set affinity mask to include CPUs 0 to 7. */

	 CPU_ZERO(&cpuset);
	 for (size_t j = 0; j < 8; j++)
			 CPU_SET(j, &cpuset);

	 s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
	 if (s != 0)
			 handle_error_en(s, "pthread_setaffinity_np");

	 /* Check the actual affinity mask assigned to the thread. */

	 s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
	 if (s != 0)
			 handle_error_en(s, "pthread_getaffinity_np");

	 printf("Set returned by pthread_getaffinity_np() contained:\n");
	 for (size_t j = 0; j < CPU_SETSIZE; j++)
			 if (CPU_ISSET(j, &cpuset))
					 printf("    CPU %zu\n", j);

	 exit(EXIT_SUCCESS);
}
